#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime

from tutorial.mysqlmoudel.myusesql.basesql import baseSql


class SpiderSql(baseSql):

    def __init__(self):
        pass

    def savecommentitem(self, items):
        bool,sql = self.insert_more_string(items)
        if bool:
            sql = "REPLACE INTO `mws_review` "+sql
        else:
            assert "sql err,please check,at spidersql file and savecommentitem method,insert_more_string method"
        # print sql
        return sql

    def getproxyip(self):
        return "SELECT id,ip,port FROM mws_proxy_ips WHERE `work`='1' AND source='1' ORDER BY usetimes ASC LIMIT 0,1;"

    def updateproxyipusetime(self,id):
        return "update mws_proxy_ips set usetimes = usetimes+1 WHERE `id` = %s" % id

    def joinproxyip(self,ipandport):
        ips = ipandport.split(':')
        ip = ips[0]
        port = ips[1]
        print ip,port
        sql = "INSERT INTO `mws_proxy_ips` (ip,port,created_at,updated_at,`work`,errtimes,usetimes,lapseTimes,source) VALUE ('%s','%s','%s','%s',1,0,0,0,'1')" % (ip,port,datetime.datetime.now(),datetime.datetime.now())
        return sql

    def deletemyip(self):
        sql = "DELETE FROM `mws_proxy_ips` WHERE source = '1'"
        return sql

